import Vue from 'vue'
import Router from 'vue-router'
import store from './store/index.js'
import Cookie from "js-cookie"
import global from './utils/global.js'

Vue.use(Router);


const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'route',
      component: () => import('./views/index.vue'),
      redirect: '/dashboard',
      meta: { keep_limit: true, title: '视图入口' }
    },
    {
      path: '/globalModel',
      name: 'globalModel',
      component: () => import('./views/globalModel/globalModel.vue'),
      meta: { keep_limit: false, title: '首页' },
      children: [
        {
          path: '/dashboard',
          name: 'dashboard',
          component: () => import('./views/dashboard/dashboard.vue'),
          meta: { keep_limit: true, title: '仪表盘' },
        },
        {
          path: '/production',
          name: 'production',
          component: () => import('./views/production/production.vue'),
          meta: { keep_limit: true, title: '商品' },
          children: [
            {
              path: '/production/management',
              name: 'p_management',
              component: () => import('./views/production/module/p_management.vue'),
              meta: { keep_limit: true, title: '商品管理' },
            },
            {
              path: '/production/add',
              name: 'p_add',
              component: () => import('./views/production/module/p_add.vue'),
              meta: { keep_limit: true, title: '商品添加' },
            },
            {
              path: '/production/review',
              name: 'p_review',
              component: () => import('./views/production/module/p_review.vue'),
              meta: { keep_limit: true, title: '商品评论' },
            },
          ]
        },
        {
          path: '/order',
          name: 'order',
          component: () => import('./views/order/order.vue'),
          meta: { keep_limit: true, title: '订单' },
          children: [
            {
              path: '/order/management',
              name: 'orderManagement',
              component: () => import('./views/order/module/o_management.vue'),
              meta: { keep_limit: true, title: '订单管理' },
            },
            {
              path: '/cash_order',
              name: 'cash_order',
              component: () => import('./views/order/module/cash_order.vue'),
              meta: { keep_limit: true, title: '收银订单' },
            },
            {
              path: '/order2',
              name: 'order2',
              component: () => import('./views/order/module/order2.vue'),
              meta: { keep_limit: true, title: '售后订单' },
            }
          ]
        },
        {
          path: '/user',
          name: 'user',
          component: () => import('./views/user/user.vue'),
          meta: { keep_limit: true, title: '用户' },
          children: [
            {
              path: '/user/management',
              name: 'userManagement',
              component: () => import('./views/user/module/management.vue'),
              meta: { keep_limit: true, title: '用户管理' },
            },
          ]
        },
        {
          path: '/statistics',
          name: 'statistics',
          component: () => import('./views/statistics/statistics.vue'),
          meta: { keep_limit: false, title: '统计' },
          children: [
            {
              path: '/statistics/production',
              name: 'statistics_production',
              component: () => import('./views/statistics/module/production.vue'),
              meta: { keep_limit: true, title: '商品统计' },
            },
          ]
        },
        {
          path: '/userCenter',
          name: 'userCenter',
          component: () => import('./views/userCenter/userCenter.vue'),
          meta: { keep_limit: true, title: '个人中心' },
        },
        {
          path: '/specialEffects',
          name: 'specialEffects',
          component: () => import('./views/specialEffects/specialEffects.vue'),
          meta: { keep_limit: true, title: 'js+css动画特效' },
          children: [
            {
              path: '/specialEffects/textBounce',
              name: 'textBounce',
              component: () => import('./views/specialEffects/module/text.vue'),
              meta: { keep_limit: true, title: '文字特效' },
            },
            {
              path: '/specialEffects/waterfallFlow',
              name: 'waterfallFlow',
              component: () => import('./views/specialEffects/module/waterfallFlow.vue'),
              meta: { keep_limit: true, title: '瀑布流' },
            },
          ]
        }
      ]
    },
    {
      path: '/screenMonitor',
      name: 'screenMonitor',
      component: () => import('./views/screenMonitor/screenMonitor.vue'),
      meta: { keep_limit: false, title: '大屏模板' }
    },
    {
      path: '/screen',
      name: 'screen',
      component: () => import('./views/screenMonitor/screen.vue'),
      meta: { keep_limit: true, title: '大屏监控' }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('./views/login/login.vue'),
      meta: { keep_limit: false, title: '' }
    },
    {
      path: '/backPwd',
      name: 'backPwd',
      component: () => import('./components/backPwd/backPwd.vue'),
      meta: { keep_limit: false, title: '找回密码' }
    },
    {
      path: '/404',
      name: '404',
      component: () => import('./views/404.vue'),
      meta: { keep_limit: false, title: '' }
    }
  ]
});

router.beforeEach((to, from, next) => {
  let token = Cookie.get(global.tokenTag);
  // 先判断页面是否需要权限，再判断是否有token且是否有权限
  if(to.meta.keep_limit) {
    if(token) {
      if(to.meta.title) {
        document.title = 'LET后台系统——' + to.meta.title
      }
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
});

export default router
